package com.bor.gb.admin.ui;

import com.bor.gb.db.domain.Guide;
import com.bor.gb.db.domain.Place;
import com.bor.gb.db.repositories.GuideRepository;
import com.bor.gb.db.repositories.PlaceRepository;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import ru.xpoft.vaadin.VaadinView;

import java.util.List;

@Component
@Scope("prototype")
@VaadinView(ArticleView.NAME)
public class ArticleView extends VerticalLayout implements View {

    public static final String NAME = "ArticleView";

    @Autowired
    private transient GuideRepository repository;

    @Autowired
    private transient PlaceRepository placeRepository;

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {


        HorizontalLayout content = new HorizontalLayout();
        content.setSpacing(false);
        addComponent(content);

        VerticalLayout artEditorLayout = new VerticalLayout();

        final RichTextArea textArea = new RichTextArea();
        artEditorLayout.addComponent(textArea);

        final TextField textField = new TextField();
        artEditorLayout.addComponent(textField);

        final ComboBox placeSelector = new ComboBox();

        artEditorLayout.addComponent(placeSelector);

        // build Tree
        final Tree artsTree = new Tree();

        for (Place place : placeRepository.findAll()) {
            artsTree.addItem(place);
            placeSelector.addItem(place);

            artsTree.setChildrenAllowed(place, true);
        }

        artsTree.addExpandListener(new Tree.ExpandListener() {
            @Override
            public void nodeExpand(Tree.ExpandEvent event) {
                Place place = (Place) event.getItemId();

                List<Guide> guides = repository.findGuidesByPlace(place);
                for (Guide guide : guides) {
                    artsTree.addItem(guide);
                    artsTree.setParent(guide, place);
                    artsTree.setChildrenAllowed(guide, false);

                }

            }
        });

        artsTree.addItemClickListener(event1 -> {
            Object data = event1.getItemId();

            if (data instanceof Guide) {
                Guide guide = (Guide) data;
                textArea.setValue(guide.getName());
                textField.setValue(guide.getUrl());
            }

        });
        content.addComponent(artsTree);

        // build Article Editor


        Button bt = new Button("Save");
        bt.addClickListener(event2 -> {
            //TODO SMTH with it
            //repository.save(new Guide(textField.getValue(), textArea.getValue(), (Place)placeSelector.getValue()));
            Notification.show("Saved");

        });


        artEditorLayout.addComponent(bt);

        content.addComponent(artEditorLayout);

    }
}

